C
      SUBROUTINE COMPMP (IX, IY, LY, CLASS, NREC, NEL, M, N)
C
C     THIS ROUTINE FINDS AND PRINTS THE JOINT HISTOGRAM
C     BETWEEN TWO MAPS, EACH WITH NREC LINES AND NEL PIXELS PER LINE.
C     THE FIRST MAP SHOULD HAVE M CLASSES OR LESS, AND THE SECOND, N OR
C     LESS. INPUT MAPS 1 AND 2 ON UNITS 8 AND 11 HAVE NUMBERS 0 THRU M,
C     0 THRU N RESPECTIVELY (UNFORMATTED FORTRAN).
C
C     THE OUTPUTS OF THIS ROUTINE ARE:
C            1) PRINT OF THE JOINT HISTOGRAM, THE NUMBER AND PERCENTAGE
C            OF CORRECT CLASSIFICATIONS, ERRORS AT BOUNDARY POINTS AND
C            ERRORS AT INTERIOR POINTS;
C            2) OUTPUT MAP ON UNIT 12 SHOWING THE TYPES OF ERRORS.
C
      LOGICAL*1 IX(NEL,3), IY(NEL), LY(NEL), BDY, TITLE(72)
      DIMENSION IH(4), PCTACC(21)
      COMMON/CONTAB/JNTH(21,21),IH1(21),IH2(21),PERCNT(21),PCTIH2(21),
     .   CT(21,21),NTOT
      INTEGER POINT(3)
      DOUBLE PRECISION CLASS(1), TYPE(4), INY, PCT
      LOGICAL*1 FMT1(22), FMT2(20), DIGITS(10)
      DATA TYPE /'EXTERIOR', 'NO ERROR', 'BOUNDARY', 'NO BOUND'/
      DATA INY, PCT /'INVNTORY', ' PERCENT'/
      DATA DIGITS /'0123456789'/, FMT1 /'(1H+,29X, (10X),F10.2)'/,
     .FMT2 /'(1H+,29X, (10X),I10)'/
C
      DO 1 I=1,M
      DO 1 J=1,N
    1 JNTH(I,J) = 0
      DO 2 I=1,4
    2 IH(I) = 0
C
      CALL READAR (8, IX, NEL)
      DO 3 I=1,NEL
    3 IX(I,2) = IX(I,1)
      DO 5 I=1,3
    5 POINT(I) = I
C
      DO 11 I=1,NREC
      K1 = POINT(1)
      K2 = POINT(2)
      K3 = POINT(3)
      IF (I.LT.NREC) CALL READAR (8, IX(1,K3), NEL)
      READ (11) IY
C
      DO  22 J=1,NEL
      LY(J) = 1
      K=IX(J,K2)
      L=IY(J)
C
C     CHECK IF EXTERIOR PIXEL IN EITHER MAP
      IF (L.EQ.0.OR.K.EQ.0) GO TO 30
C
C     GENERATE CLASS ASSIGNMENT MATRIX
      JNTH(K,L) = JNTH(K,L) + 1
C
C     CHECK WHETHER CLASS NUMBERS AGREE
      IF (K.EQ.L) GO TO 35
C
C     CHECK WHETHER ANY OF THE 4 NEAREST NEIGHBORS OF THE IX PIXEL
C     ARE A DIFFERENT CLASS
                 BDY = IX(J,K2).NE.IX(J,K1)
     .             .OR.IX(J,K2).NE.IX(J,K3)
     ..OR.J.GT.1  .AND.IX(J,K2).NE.IX(J-1,K2)
     ..OR.J.LT.NEL.AND.IX(J,K2).NE.IX(J+1,K2)
      IF (BDY) LY(J) = 2
      IF (.NOT.BDY) LY(J) = 3
      GO TO 35
C
   30 LY(J) = 0
   35 IW = LY(J) + 1
      IH(IW)=IH(IW)+1
   22 CONTINUE
      DO 25 J=1,3
   25 POINT(J) = MOD(POINT(J),3) + 1
   11 WRITE (12) LY
C
C     FIND NUMBER OF NON-EXTERIOR PIXELS AND CLASSIFICATION ACCURACY
      NTOT = NREC*NEL - IH(1)
      FAC=100./NTOT
      ACC = FAC * IH(2)
C
C     FIND CLASS OCCUPANCIES IN MAP 1 AND MAP 2
      DO 9 I=1,M
    9 IH1(I) = 0
      DO 10 I=1,M
      DO 10 J=1,N
   10 IH1(I) = IH1(I) + JNTH(I,J)
      DO 19 J=1,N
   19 IH2(J) = 0
      DO 20 J=1,N
      DO 20 I=1,M
   20 IH2(J) = IH2(J) + JNTH(I,J)
C
C     FIND SIMILARITY MEASURES BASED ON CLASS POPULATIONS ONLY.
      MINC = MIN0 (M, N)
      INV = 0
      DO 65 I=1,MINC
   65 INV = INV + MIN0 (IH1(I), IH2(I))
      SIM = FAC * INV
C
C     PRINT HISTOGRAM AND PERCENTAGE OCCUPANCIES
      FMT1(10) = DIGITS(N)
      FMT2(10) = DIGITS(N)
      READ (5,100) TITLE
      WRITE (6,200) TITLE
      WRITE (6,400) NREC, NEL, M, N
      WRITE (6,300) NTOT
      WRITE (6,500)
      WRITE (6,102) (CLASS(J), J=1,N), INY, PCT
      DO 70 J=1,N
   70 PCTIH2(J) = FAC * IH2(J)
      DO 80 I=1,M
      PERCNT(I) = FAC * IH1(I)
      PCTACC(I) = 100.0 * JNTH(I,I) / IH1(I)
      WRITE (6,101) CLASS(I), (JNTH(I,J), J=1,N), IH1(I)
   80 WRITE (6,FMT1) PERCNT(I)
      WRITE (6,101) INY, (IH2(J), J=1,N), NTOT
      WRITE (6,FMT1) ACC
      WRITE (6,103) PCT, (PCTIH2(J), J=1,N), SIM
      WRITE (6,FMT2) IH(2)
      WRITE (6,104) (PCTACC(J), J=1,N)
      WRITE (6,600)
      DO 90 I=2,4
      PRCNT=IH(I)*FAC
   90 WRITE(6,700) TYPE(I),IH(I),PRCNT
      WRITE (6,700) TYPE(1), IH(1)
      RETURN
C
  100 FORMAT (72A1)
  101 FORMAT ('0',A9,11I10)
  102 FORMAT (/11X,12A10)
  103 FORMAT ('0',A9,11F10.2)
  104 FORMAT ('0 ACCURACY',11F10.2)
  200 FORMAT ('1',5X,72A1)
  300 FORMAT ('0  TOTAL NO. OF VALID (NON-EXTERIOR) POINTS =',I7)
  400 FORMAT ('0  IMAGE SIZE='I5,' BY'I5,'. NUMBERS OF CLASSES IN MAPS 1
     . AND 2 ARE'I3,' AND'I3,'.')
  500 FORMAT (//30X,19('*')/30X,'* JOINT HISTOGRAM *'/30X,19('*'))
  600 FORMAT (//10X,'TYPE',5X,'NO. OF OCCURRENCES',5X,'PERCENTAGE'/)
  700 FORMAT (/A16,I15,F20.2)
      END
C
      SUBROUTINE READAR (NTAPE1, W, NSAMP)
C
C  READ NSAMP BYTES INTO ARRAY W FROM LOGICAL UNIT NTAPE1
C
      LOGICAL*1 W(NSAMP)
C
      READ (NTAPE1) W
      RETURN
C
C     ******************************************************************
C
      ENTRY WRITAR (NTAPE1, W, NSAMP)
C
C  WRITE NSAMP BYTES FROM ARRAY W ONTO LOGICAL UNIT NTAPE1
C
      WRITE (NTAPE1) W
      RETURN
      END
